package com.java.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.java.service.AdminService;
import com.java.util.resp.BaseRespone;
import com.java.util.servlet.ServletUtil;

@Controller
public class IndexController {
	private final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired private AdminService adminService;
	@Autowired HttpServletRequest request;

	@RequestMapping("/")
	public String def() {
		log.info("def");
		if(ServletUtil.adminIsLogin()) {
			return "admin/info";
		}
		return "admin/login";
	}

	@RequestMapping(value = "/admin/{fileName}", method = RequestMethod.GET)
	public String admin(@PathVariable String fileName) {
		if(fileName.equals("login") && ServletUtil.adminIsLogin()) {
			return "admin/info";
		}
		return "admin/" + fileName;
	}

	@RequestMapping(value = "/admin/logon", method = RequestMethod.POST)
	@ResponseBody
	public BaseRespone adminLogon(String user, String password) {
		Object URI = request.getSession().getAttribute("URI");
		BaseRespone resp = adminService.login(user, password);
		if(null != URI) {
			resp.setData(URI.toString());
		}
		return resp;
	}

	@RequestMapping(value = "/admin/logout", method = RequestMethod.GET)
	public String adminlogout() {
		ServletUtil.getSession().invalidate();
		return "admin/login";
	}

	@RequestMapping(value = "/user/{fileName}", method = RequestMethod.GET)
	public String user(@PathVariable String fileName) {
		if(fileName.equals("login") && ServletUtil.userIsLogin()) {
			return "user/info";
		}
		return "user/" + fileName;
	}
}
